package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.MenuDao;
import com.djinfo.work.dao.RoleMenuDao;
import com.djinfo.work.entity.TbSysMenu;
import com.djinfo.work.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 *
 */
@Service("MenuService")
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuDao meuDao;

	@Autowired
	private RoleMenuDao roleMenuDao;

	@Override
	public void save(TbSysMenu TbSysMenu) {
		meuDao.save(TbSysMenu);
	}
	@Override
	public void update(TbSysMenu TbSysMenu) {
		meuDao.update(TbSysMenu);
	}

/*	@Override
	public void delete(TbSysMenu TbSysMenu) {
		meuDao.delete(TbSysMenu);
	}*/

	@Override
	public void deleteMenuByParentAuthId(String id) {
		meuDao.deleteMenuByParentAuthId(id);
	}

	@Override
	public TbSysMenu selectOne(TbSysMenu TbSysMenu) {
		return meuDao.selectOne(TbSysMenu);
	}

	@Override
	public TbSysMenu getMenuById(String menuId) {
		return meuDao.getMenuById(menuId);
	}

	@Override
	public List<TbSysMenu> page(TbSysMenu TbSysMenu, Integer pageNum, Integer pageSize) {
		return meuDao.page(TbSysMenu,  pageNum,  pageSize);
	}

	@Override
	public List<TbSysMenu> list(TbSysMenu menu) {
		return meuDao.list(menu);
	}

	@Override
	public Integer getAllRowCount() {
		return meuDao.getAllRowCount();
	}

	@Override
	public Integer getRowCount(TbSysMenu tbSysMenu) {
		return meuDao.getRowCount(tbSysMenu);
	}

	@Override
	public List<TbSysMenu> getMenuByLevel(Integer level) {
		return meuDao.getMenuByLevel(level);
	}

	@Override
	public List<TbSysMenu> getLevel1List(TbSysMenu menu) {
		return meuDao.getLevel1List(menu);
	}

	@Override
	public AjaxJson list(TbSysMenu menu, Integer pagenum, Integer pagesize) {
		HashMap<String, Object> map = new HashMap<>();
		AjaxJson ajaxJson = new AjaxJson();
		try {
			List list2=new ArrayList();
			List<TbSysMenu> list=new ArrayList<TbSysMenu>();
			menu.setParentAuthId("0");
			List<TbSysMenu> res = meuDao.page(menu,pagenum,pagesize);
			List<TbSysMenu> allMenuList = meuDao.getAllMenuList();
			for(TbSysMenu tbSysMenu :res){
				list.add(tbSysMenu);
				addLeafMenuToList(list,tbSysMenu,allMenuList);
            }
			Integer count = meuDao.getRowCount(menu);
			map.put("dataList",list);
			map.put("total",count);
			ajaxJson.setResult(map);
		} catch (Exception e) {
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	public void addLeafMenuToList(List list,TbSysMenu parentMenu,List<TbSysMenu> allMenuList){
		for (TbSysMenu temp:allMenuList) {
			if(temp.getParentAuthId().equals(parentMenu.getPkMenuId())){
				list.add(temp);
				if(!temp.getIsLeaf().equals("1")){
					addLeafMenuToList(list,temp,allMenuList);
				}
			}
		}
	}

	@Override
	public AjaxJson getLevel1MenuList(TbSysMenu menu) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			menu.setParentAuthId("0");
			List list=meuDao.getMenuByLevel(menu.getLevel());
			TbSysMenu tbSysMenu =new TbSysMenu();
			tbSysMenu.setMenuName("将该菜单设置为一级");
			tbSysMenu.setParentAuthId("0");
			tbSysMenu.setPkMenuId("0");
			list.add(0, tbSysMenu);
			HashMap map=new HashMap();
			map.put("list",list);
			ajaxJson.setResult(map);
		} catch (Exception e) {
			ajaxJson.setCode(301);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson saveOrUpdate(TbSysMenu menu, HttpServletRequest request) {
		AjaxJson ajaxJson = null;
		try {
			String userId=(String) request.getSession().getAttribute("userId");
			String nowTime= StringUtil.getNowTime();
			ajaxJson = new AjaxJson();
			if(menu.getPkMenuId()==null||"".equals(menu.getPkMenuId())){
                menu.setPkMenuId(null);
                menu.setCreateTime(nowTime);
                menu.setUpdateTime(nowTime);
                menu.setCreateUser(userId);
                menu.setUpdateTime(userId);
                meuDao.save(menu);
            }else {
                menu.setUpdateTime(nowTime);
                menu.setUpdateUser(userId);
                meuDao.update(menu);
            }
		} catch (Exception e) {
			ajaxJson.setCode(301);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson delete(TbSysMenu menu) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			TbSysMenu menu1=meuDao.getMenuById(menu.getPkMenuId());
			if("0".equals(menu1.getParentAuthId())){

                meuDao.delete(menu);
                meuDao.deleteMenuByParentAuthId(menu.getPkMenuId());
				List<TbSysMenu> list=meuDao.getMenuListByParentId(menu.getPkMenuId());
				for(TbSysMenu tbSysMenu:list){
					roleMenuDao.deleteRoleMenuByMenuId(tbSysMenu.getPkMenuId());
				}
				roleMenuDao.deleteRoleMenuByMenuId(menu.getPkMenuId());


            }else{
                meuDao.delete(menu);
				roleMenuDao.deleteRoleMenuByMenuId(menu.getPkMenuId());
            }
		} catch (Exception e) {
			ajaxJson.setCode(301);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}
}
